#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
  key_t key;
  int shmid;
  void *ptr, *temp;
  key = ftok("/", 'a');
  shmid = shmget(key, 1024, IPC_CREAT | 0666);
  ptr = shmat(shmid, NULL, 0);
  temp = ptr;
  while (1)
  {
    if (strcmp(temp, "exit\n") == 0)
    {
      printf("Exiting\n");
      shmdt(ptr);                    //
      shmctl(shmid, IPC_RMID, NULL); //
      exit(0);
    }
    else
    {
      printf("Data received: %s\n", (char *)temp);
      // 清空ptr的128个字节
      memset(temp, 0, 128);
    }
    sleep(1);
    temp += 128;
    if (temp >= (ptr + 1024))
    {
      temp = ptr;
    }
  }
}